package io.scnb.lesson001.demo20;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 当使用@Configuration注解的时候，spring会通过cglib给ConfigBean2类创建一个代理类
 * 代理类会拦截所有的@Bean修饰的方法，默认情况下确保这些方法只被调用一次，保障了bean是
 * 单实例的。
 */
@Configuration
public class ConfigBean2 {

    @Bean
    public ServiceA serviceA() {
        System.out.println("调用serviceA()方法");
        return new ServiceA();
    }

    @Bean
    public ServiceB serviceB1() {
        System.out.println("调用serviceB1()方法");
        ServiceA serviceA = this.serviceA();
        return new ServiceB(serviceA);
    }

    @Bean
    public ServiceB serviceB2() {
        System.out.println("调用serviceB2()方法");
        ServiceA serviceA = this.serviceA();
        return new ServiceB(serviceA);
    }
}
